define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	// 发布公告
	var Edit = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'uk-form P-messageManageEdit',
				'view_model': {
					'from': '',
					'to': [],
					'title': '',
					'content': ''
				}
			});

			this.$data = null;
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM().addClass(c.class);
			var doms = this.$doms = {};

			$([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('发布公告')+'</span>',
				'</div>',

				'<div class="M-formCon">',

					'<div class="M-formConGroup">',
						'<label class="group-label">'+LANG('收件人：')+'</label>',
						'<div class="group-content">',
							'<div class="customer">',
								'<span class="hint" ms-visible="!to.length">'+LANG('请选择收件人，收件人为空则发送给所有人')+'</span>',
								'<div class="customerItem" ms-repeat-el="to">',
									'<span ms-attr-title="el.Name">{{el.Name|truncate(20)}}</span>',
									'<span></span>',
									'<i ms-click="$remove"/>',
								'</div>',
							'</div>',

							'<div class="selectBtn">',
								'<a>'+LANG('选择联系人')+'</a>',
							'</div>',
						'</div>',
					'</div>',

					'<div class="M-formConGroup">',
						'<label class="group-label">'+LANG('标题：')+'</label>',
						'<div class="group-content">',
							'<input class="title" placeholder="'+LANG('50字以内')+'" ms-duplex="title"/>',
						'</div>',
					'</div>',

					'<div class="M-formConGroup">',
						'<label class="group-label">'+LANG('内容：')+'</label>',
						'<div class="group-content" pub-name="ueditor" pub-mod="modules/ueditor.base"></div>',
					'</div>',
				'</div>',

				'<div class="M-formButtons">',
					'<span pub-name="cancel" pub-mod="@base/common/input.button" ></span>',
					'<span pub-name="save" pub-mod="@base/common/input.button" ></span>',
				'</div>'
			].join('')).appendTo(el);

			doms.customers = el.find('.customers');

			var config = {
				'ueditor': {
					'height': 380,
					'width': '100%'
				},
				// 表单取消按钮
				'cancel': {
					'value': LANG('取消'),
					'class': 'btn btn-link mr20'
				},
				// 表单保存按钮
				'save': {
					'value': LANG('发送'),
					'class': 'btn btn-confirm'
				}
			}
			self.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){
			var self = this;
			self.uiBind(self.find('.selectBtn > a'), 'click', 'eventShowCustomerList');
		},
		eventShowCustomerList: function(evt, elm){
			this.selectCustomer();
			return false;
		},
		// 打开添加客户账号popwin
		selectCustomer: function(){
			var dlg = this.get('addCustomerEmailPopwin');
			if(!dlg){
				this.createAsync('addCustomerEmailPopwin', 'admin/messageManage/popwin/popwin.selectCustomerPopwin',
					{
						'grid':{
							pager: {
								'showSizeTypes': 1
							}
						}
					},
					function(mod){
					mod.show();
				});
			}else{
				dlg.show();
			}
		},
		// 添加账号响应
		onSelectCustomer: function(ev){
			this.vm.$.to = ev.param.concat([]);
		},
		onInputClick: function(ev){
			switch (ev.name){
				case 'cancel':
					this.cancel();
					break;
				case 'save':
					this.save();
					break;
			}
			return false;
		},
		setData: function(data){
			return this;
		},
		getData: function(){
			var self = this;

			var to = [];
			util.each(this.vm.getItem('to'), function(item){
				if(item && item.UserId){
					to.push(item.UserId);
				}
			});
			var user = pubjs.getUser();
			var from = (user && user.login && user.login.email) || '';

			var data = {
				'From': from,
				'Type': 1, // 消息类型，1：人工消息.......
				'To': to,
				'Title': util.trim(self.vm.$.title),
				'Content': self.$.ueditor.getData()
			};
			return data;
		},
		save: function(){
			var self = this;
			var data = self.getData();
			if(!data.Title){
				pubjs.alert(LANG('请输入公告标题！'), function(){
					self.getDOM().find('.title').focus();
				});
				return false;
			}
			if(!data.Content){
				pubjs.alert(LANG('请输入公告内容！'));
				return false;
			}
			pubjs.loading.show();
			pubjs.mc.send('message/save', data, self.onSave.bind(self));
		},
		onSave: function(err, data){
			pubjs.loading.hide();
			if (err){
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert(LANG('发布成功！'));
			pubjs.controller.navigate('admin/messageManage');
		},
		cancel: function(){
			pubjs.confirm(LANG('真的要取消吗？'), function(isOk){
				if(isOk){
					pubjs.controller.navigate('admin/messageManage');
				}
			});
		},
		reset: function(){
			this.vm.reset();
			this.$.ueditor.reset();
		}
	});
	exports.main = Edit;

	// 产品管理员-查看消息详情
	var ManageCheck = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'uk-form P-messageManageCheck',
				'view_model': {
					'to': '',
					'title': '',
					'content': '',
					'time': ''
				}
			});

			this.$data = null;
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM().addClass(c.class);

			$([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('公告详情')+'</span>',
					'<span pub-name="back" pub-mod="@base/common/input.button" ></span>',
				'</div>',

				'<div class="P-messageManageCheckMessage">',
					'<div class="messageItem mt30">',
						'<label class="toLabel">'+LANG('收件人：')+'</label>',
						'<div class="toCon">',
							'<span ms-text="to"/>',
						'</div>',
					'</div>',
					'<div class="messageItem">',
						'<label class="">'+LANG('标题：')+'</label>',
						'<span ms-text="title"/>',
					'</div>',
					'<div class="messageItem">',
						'<label class="">'+LANG('时间：')+'</label>',
						'<span>{{time}}</span>',
					'</div>',
					'<div class="messageItem">',
						'<label class="">'+LANG('正文：')+'</label>',
						'<div class="contentCon" ms-html="content"/>',
					'</div>',
				'</div>'
			].join('')).appendTo(el);

			var config = {
				// 表单返回按钮
				'back': {
					'value': LANG('返回'),
					'icon': 'uk-icon-chevron-left',
					'class': 'btn btn-default mr20 mt10'
				}
			}

			self.buildFromTemplate(config);
		},
		onInputClick: function(ev){
			if(ev.name == 'back'){
				pubjs.controller.navigate('admin/messageManage');
			}
			return false;
		},
		load: function(id){
			if(id){
				pubjs.loading.show();
				pubjs.mc.send('message/load', {'id': id}, this.onData.bind(this));
			}
		},
		onData: function(err, data){
			pubjs.loading.hide();
			if (err){
				pubjs.alert(err.message);
				return false;
			}
			this.setData(data);
		},
		setData: function(data){
			if(data){
				var vm = this.vm.$;
				if(data.To && !data.To.length){
					vm.to = LANG('所有人');
				}else{
					vm.to = data.Recipient && data.Recipient.join('、') || '';
				}
				vm.title = data.Title || '';
				vm.time = util.date('Y-m-d H:i:s',data.UpdateTime) || '';
				vm.content = data.Content || '';
			}
			return this;
		},
		reset: function(){
			this.vm.reset();
			return this;
		}
	});
	exports.manageCheck = ManageCheck;

	// 普通-查看消息详情
	var Check = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'uk-form P-messageManageCheck',
				'hasBackBtn': true,
				'view_model': {
					'to': '',
					'title': '',
					'content': '',
					'time': ''
				}
			});

			this.$data = null;
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM().addClass(c.class);

			$([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('公告详情')+'</span>',
					'<span pub-name="back" pub-mod="@base/common/input.button" class="backBtn"></span>',
				'</div>',

				'<div class="P-messageManageCheckMessage">',
					'<div class="messageItem mt30"  ms-visible="false">',
						'<label class="toLabel">'+LANG('收件人：')+'</label>',
						'<div class="toCon">',
							'<span ms-text="to"/>',
						'</div>',
					'</div>',
					'<div class="messageItem mt30">',
						'<label class="">'+LANG('标题：')+'</label>',
						'<span ms-text="title"/>',
					'</div>',
					'<div class="messageItem">',
						'<label class="">'+LANG('时间：')+'</label>',
						'<span>{{time}}</span>',
					'</div>',
					'<div class="messageItem">',
						'<label class="">'+LANG('正文：')+'</label>',
						'<div class="contentCon" ms-html="content"/>',
					'</div>',
				'</div>'
			].join('')).appendTo(el);

			el.find('.M-commonFormHeader').toggle(c.hasBackBtn);

			var config = {
				// 表单返回按钮
				'back': {
					'value': LANG('返回'),
					'icon': 'uk-icon-chevron-left',
					'class': 'btn btn-default mr20 mt10'
				}
			}

			self.buildFromTemplate(config);
		},
		onInputClick: function(ev){
			if(ev.name == 'back'){
				pubjs.controller.navigate('user/message');
			}
			return false;
		},
		load: function(id){
			if(id){
				pubjs.loading.show();
				pubjs.mc.send('usermessage/load', {'id': id}, this.onData.bind(this));
			}
		},
		onData: function(err, data){
			pubjs.loading.hide();
			if (err){
				pubjs.alert(err.message);
				return false;
			}

			this.setData(data);

			var param = {
				'ids': [data._id],
				'is_noticed': 1,
				'is_read': 1
			}
			pubjs.mc.send('usermessage/update', param, this.onNoticed.bind(this));
		},
		onNoticed: function(err, data){
			if (err){
				pubjs.alert(err.message);
				return false;
			}
			// 提醒图标减少红色数字
			this.fire('setNoticed',data);
		},
		setData: function(data){
			if(data){
				var vm = this.vm.$;
				if(data.To && !data.To.length){
					vm.to = LANG('所有人');
				}else{
					vm.to = data.Recipient && data.Recipient.join('、') || '';
				}
				vm.title = data.Title || '';
				vm.time = util.date('Y-m-d H:i:s',data.UpdateTime) || '';
				vm.content = data.Content || '';
			}
			return this;
		},
		reset: function(){
			this.vm.reset();
		}
	});
	exports.check = Check;

});